package com.yc.book.dao;

import java.sql.Date;
import java.sql.SQLException;
import java.util.*;

import com.yc.api.d1026.DBHelper;
import com.yc.book.biz.BizException;

public class BookDAO {
	
	public List<Map<String,Object>> select(String name, String author, String prices){
		// 组合条件查询
		String sql = "select * from book where 1=1";
		ArrayList<Object> params = new ArrayList<Object>();
		if(name!=null && name.isEmpty() == false) {
			sql += "and name like '%' || ? || '%'";
			params.add(name);
		}
		if(author!=null && author.isEmpty() == false) {
			sql += "and author like '%' || ? || '%'";
			params.add(author);
		}
		if(prices!=null && prices.isEmpty() == false) {
			sql += "and price between ? and ?";
			// JDBC 会自动对类型进行转换
			String priceStr[] = prices.split("~");
			params.add(priceStr[0]);
			params.add(priceStr[1]);
		}
		
		sql += " order by id";
		
		try {
			return DBHelper.selectListMap(sql, params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Map<String,Object>>();
		}
		
	}
	
	public void insert(String id, String name, String author, 
			Date pressdate, int price) throws SQLException {
		String sql = "insert into book values (seq_pub.nextval,?,?,null,?,default,?,1)";
		DBHelper.update(sql, name, author, pressdate, price);
	}

	public void update(String id, String name, String author, Date pressdate, int price)
			throws SQLException {
		String sql = "update book set name=?, author=?, pressdate=?, price=? where id=?";
		DBHelper.update(sql, name, author, pressdate, price, id);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	

}
